package com.leboncoin.bean;

import com.leboncoin.util.FormatManager;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "searchitem")
public class SearchItemBean implements Serializable {

    @Transient
    protected static final Logger logger = Logger.getLogger(SearchItemBean.class.getName());

    @Id
    String searchText;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "searchItemBean")
    List<ClassifiedBean> searchResults = new ArrayList<>();

    String searchCategory; //'Ventes immobilieres' 'Voitures'

    //@Column(name = "avg_price")
    @Transient
    Long average;

    @Transient
    Integer priceMin;

    @Transient
    Integer priceMax;

    public enum LocationType {

        PUY_DE_DOME("auvergne/puy_de_dome"), DEP_VOISINS("auvergne/puy_de_dome/63"), AUVERGNE("auvergne"),
        FRANCE("auvergne/occasions");

        private LocationType(String name) {
            this.name = name;
        }
        private final String name;

        @Override
        public String toString() {
            return name;
        }
    };
    @Transient
    LocationType searchLocation;

    @Transient
    List<String> zipCodes = new ArrayList<>();
    @Transient
    List<String> checkBoxList = new ArrayList<>();

    public String getSearchCategory() {
        return searchCategory;
    }

    public void setSearchCategory(String searchCategory) {
        this.searchCategory = searchCategory;
    }

    public String getSearchText() {
        return searchText;
    }

    public String getSearchTextUrlEncoded() throws UnsupportedEncodingException {
        return URLEncoder.encode(searchText, "UTF-8");
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }

    public String getPriceMinAsString() {
        return FormatManager.formatNumberWithSpaces(getPriceMin());
    }

    public Integer getPriceMin() {
        return priceMin;
    }

    public void setPriceMin(Integer priceMin) {
        this.priceMin = priceMin;
    }

    public String getPriceMaxAsString() {
        return FormatManager.formatNumberWithSpaces(getPriceMax());
    }

    public Integer getPriceMax() {
        return priceMax;
    }

    public void setPriceMax(Integer priceMax) {
        this.priceMax = priceMax;
    }

    public LocationType getSearchLocation() {
        return searchLocation;
    }

    public void setSearchLocation(LocationType searchLocation) {
        this.searchLocation = searchLocation;
    }

    public List<String> getZipCodes() {
        return zipCodes;
    }

    public void setZipCodes(List<String> zipCodes) {
        this.zipCodes = zipCodes;
    }

    public List<ClassifiedBean> getSearchResults() {
        return searchResults;
    }

    public void setSearchResults(List<ClassifiedBean> searchResults) {
        this.searchResults = searchResults;
    }

    public List<String> getCheckBoxList() {
        return checkBoxList;
    }

    public void setCheckBoxList(List<String> checkBoxList) {
        this.checkBoxList = checkBoxList;
    }

    public Long getAverage() {
        return average;
    }

    public void setAverage(Long average) {
        this.average = average;
    }

}
